import java.util.Date;

public class Borrowing {

	private Item item;
	private UserAccount user;
	private Date borrowingDate;
	
	public Borrowing(Item item, UserAccount user){
		if(item==null || user==null){
			throw new NullPointerException("I got null as argument!");
		}
		this.item = item;
		this.user = user;
		this.borrowingDate = new Date();
	}
	
	public Item getBorrowedItem(){
		return item;
	}
	
	public UserAccount getBorrowingUserAccount(){
		return user;
	}
	
	public Date getBorrowingDate(){
		return new Date (this.borrowingDate.getTime());
	}
	
	public double getOverdraftFee(){
		long actualTime = new Date().getTime();
		long borrowedTime = (actualTime - getBorrowingDate().getTime())/86400000L;
		long overBorrowedTime = borrowedTime - item.getMaxBorrowTime()/86400000L;
		
		if(overBorrowedTime < 0)
			return 0.0;
		else{
			return (new Long(overBorrowedTime).doubleValue() * item.getFeePerDay());
		}
	}
	
	protected void finalize(){
		this.user.addToAccumulatedOverdraftFee(getOverdraftFee());
	}
	
}
